﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.IO;
using ReflectionStudio.Core;
using ReflectionStudio.Core.Events;
using ReflectionStudio.Core.FTPClient;
using System.Data.OracleClient;
using System.IO;
namespace WPFExcelReport
{
    struct vvPACK
    {
        public string PackNr;
        public SortedList<string, DataRow> WBBSTnr;
    }
    struct vvVKPOS
    {
        public DataRow dwVKPOS;
        public SortedList<string, vvPACK> listVVPACK;
    }
    class DSTDNVRReport : BaseReport
    {
        #region ----------------PROPERTIES----------------------------------
        byte[] b;
        public IntexReptServBLClient proxy;
        public OracleConnection OracleDB;        
        string DocType;
        string CellName = "";
        string CellValue = "";
        string strCompany;
        string orderComments = "备注：";
        string strExcelFile;
        public string Customer = "";
        public string Address = "";
        string DateCurrent = "";
        public string PrintType = "JustExcel";
        public List<vvVKPOS> listVKPOS = new List<vvVKPOS>();
        public FTPClient ftpClient;
        #endregion
        public DSTDNVRReport()
            : base()
        {

        }
        public DSTDNVRReport(Dictionary<string, object> dicParamater)
            : base(dicParamater)
        {
            DocType = dicParamater["DocType"].ToString().Trim();
            strCompany = dicParamater["Company"].ToString().Trim();
            strExcelFile = dicParamater["ExcelFile"].ToString().Trim();
            DateCurrent = DateTime.Now.Year.ToString() + "年" + DateTime.Now.Month.ToString() + "月" + DateTime.Now.Day.ToString() + "日";
            #region Excel  columns
            columns.Add("GABBUCHDAT");
            columns.Add("KDKUNDNA");
            columns.Add("CO");
            columns.Add("GABART");
            columns.Add("GABVAR");
            columns.Add("GABFARBE");
            columns.Add("GABPARTIE");
            columns.Add("GPLIEFSCH");
            columns.Add("GPSPPART");
            columns.Add("KG");
            columns.Add("GABSP");
            columns.Add("PRICE");
            columns.Add("VALUE");
            #endregion

        }

        public void FillInExcelData( DataSet setSoruce)
        {
            try
            {
                /*VKPKUNDNR,K.KDKUNDNA,C.VKPSACHBE,NAME, "
                    + " C.VKPVG,E.VGDES,T.VRKRENR,T.VRPREPOS,T.VRKREART,T.VRKREDAT, "
                    + " CO,ART,COLOR,CSART,COQTY,DEQTY,C.VKPMEEH,COPR,C.VKPPREH,C.VKPHWAEHRSL, "
                    + " COVALUE,T.INVQTY,T.VRPMEEH,T.INVPR,T.VRPPREH,T.VRPWAEHRSL,T.VALUE,
                        T.SURCHARGE1,T.SURCHARGE2,T.SURCHARGE3, "
                    + " '' AS ARTBEM, '' AS SHIPDAT,T.VRPBUSIMODE,T.PAY,'' AS INVBEM, "
                    + " '' AS REMARK1,'' AS REMARK2,'' AS REMARK3,'' AS REMARK4,'' AS REMARK5, "
                    + " '' AS REMARK6,'' AS REMARK7,'' AS REMARK8,'' AS REMARK9,'' AS REMARK10*/
                /*客户	"客户经理"	跟单员	发票号	工厂编号	客花号	规格	合同数	单位	"下单单价"	
                 * 单位	出货日期	出货数量	单位	销售金额	开票日期	开票数量		单位	货币	费用1	费用2	费用3	
                 * 开票单价	单位	开票金额	是否开清	是否已付	头备注*/

                int x = 3;
                DataTable dtSoruce = setSoruce.Tables["DNVR"];
                foreach (DataRow dataRow in dtSoruce.Rows)
                {
                    exAgent.ShowRows(x,1,x,2);
                    string KDKUNDNA = dataRow["KDKUNDNA"].ToString().Trim();
                    exAgent.SetCellValue(x, 1, KDKUNDNA);
                    string NAME = dataRow["NAME"].ToString().Trim();
                    exAgent.SetCellValue(x, 2, NAME);
                    string VGDES = dataRow["VGDES"].ToString().Trim();
                    exAgent.SetCellValue(x, 3, VGDES);
                    string INVNO = dataRow["VRKRENR"].ToString();
                    exAgent.SetCellValue(x, 4, INVNO);
                    string CO = dataRow["CO"].ToString().Trim();
                    exAgent.SetCellValue(x, 5, CO);                   
                    string CSART = dataRow["CSART"].ToString().Trim();
                    exAgent.SetCellValue(x, 6, CSART);
                    string ARTBEM = dataRow["ARTBEM"].ToString().Trim();
                    exAgent.SetCellValue(x, 7, ARTBEM);
                    string COQTY = dataRow["COQTY"].ToString().Trim();
                    exAgent.SetCellValue(x, 8, COQTY);
                    string VKPMEEH = dataRow["VKPMEEH"].ToString().Trim();
                    exAgent.SetCellValue(x, 9, VKPMEEH);
                    string COPR = dataRow["COPR"].ToString().Trim();
                    exAgent.SetCellValue(x, 10, COPR);
                    string VKPPREH = dataRow["VKPPREH"].ToString().Trim();
                    exAgent.SetCellValue(x, 11, VKPPREH);
                    string SHIPDAT = dataRow["SHIPDAT"].ToString().Trim();
                    exAgent.SetCellValue(x, 12, SHIPDAT);
                    string DEQTY = dataRow["DEQTY"].ToString().Trim();
                    exAgent.SetCellValue(x, 13, DEQTY);
                    exAgent.SetCellValue(x, 14, VKPMEEH);
                    string COVALUE = dataRow["COVALUE"].ToString().Trim();
                    exAgent.SetCellValue(x, 15, COVALUE);
                    string VRKREDAT = dataRow["VRKREDAT"].ToString().Trim();
                    exAgent.SetCellValue(x, 16, VRKREDAT);
                    string INVQTY = dataRow["INVQTY"].ToString().Trim();
                    exAgent.SetCellValue(x, 17, INVQTY);
                    string VRPMEEH = dataRow["VRPMEEH"].ToString().Trim();
                    exAgent.SetCellValue(x, 19, VRPMEEH);
                    string VRPWAEHRSL = dataRow["VRPWAEHRSL"].ToString().Trim();
                    exAgent.SetCellValue(x, 20, VRPWAEHRSL);
                    string SURCHARGE1 = dataRow["SURCHARGE1"].ToString().Trim();
                    exAgent.SetCellValue(x, 21, SURCHARGE1);
                    string SURCHARGE2 = dataRow["SURCHARGE2"].ToString().Trim();
                    exAgent.SetCellValue(x, 22, SURCHARGE2);
                    string SURCHARGE3 = dataRow["SURCHARGE3"].ToString().Trim();
                    exAgent.SetCellValue(x, 23, SURCHARGE3);
                    string INVPR = dataRow["INVPR"].ToString().Trim();
                    exAgent.SetCellValue(x, 24, INVPR);
                    string VRPPREH = dataRow["VRPPREH"].ToString().Trim();
                    exAgent.SetCellValue(x, 25, VRPPREH);
                    string VALUE = dataRow["VALUE"].ToString().Trim();
                    exAgent.SetCellValue(x, 26, VALUE);
                    string STATUS = dataRow["VRPBUSIMODE"].ToString().Trim();
                    exAgent.SetCellValue(x, 27, STATUS);
                    string PAY = dataRow["PAY"].ToString().Trim();
                    exAgent.SetCellValue(x, 28, PAY);
                    string INVBEM = dataRow["INVBEM"].ToString().Trim();
                    exAgent.SetCellValue(x, 29, INVBEM);
                    string REMARK = dataRow["REMARK"].ToString().Trim();
                    exAgent.SetCellValue(x, 30, REMARK);                    
                    /*string REMARK1 = dataRow["REMARK1"].ToString().Trim();
                    exAgent.SetCellValue(x, 30, REMARK1);
                    string REMARK2 = dataRow["REMARK2"].ToString().Trim();
                    exAgent.SetCellValue(x, 31, REMARK1);
                    string REMARK3 = dataRow["REMARK3"].ToString().Trim();
                    exAgent.SetCellValue(x, 32, REMARK1);
                    string REMARK4 = dataRow["REMARK4"].ToString().Trim();
                    exAgent.SetCellValue(x, 33, REMARK1);
                    string REMARK5 = dataRow["REMARK5"].ToString().Trim();
                    exAgent.SetCellValue(x, 34, REMARK1);
                    string REMARK6 = dataRow["REMARK6"].ToString().Trim();
                    exAgent.SetCellValue(x, 35, REMARK1);
                    string REMARK7 = dataRow["REMARK7"].ToString().Trim();
                    exAgent.SetCellValue(x, 36, REMARK1);
                    string REMARK8 = dataRow["REMARK8"].ToString().Trim();
                    exAgent.SetCellValue(x, 37, REMARK1);
                    string REMARK9 = dataRow["REMARK9"].ToString().Trim();
                    exAgent.SetCellValue(x, 38, REMARK1);
                    string REMARK10 = dataRow["REMARK10"].ToString().Trim();
                    exAgent.SetCellValue(x, 39, REMARK1);*/
                    x++;
                }
                //HideNullRows();
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void HideNullRows()
        {
            try
            {
                int HidedRows = 0;
                int rowstart = 1;
                int rowend = 1;
                int col1, col2, col3;

                var val1 = exAgent.GetCellValue(1, 1);

                rowstart = 4; rowend = 112; col1 = 11; col2 = 12;
                for (int Rowi = rowstart; Rowi <= rowend; Rowi++)
                {
                    val1 = exAgent.GetRangeValue(Rowi, Rowi, col1, col2).Trim();
                    if ((string)val1 == "0")
                    {
                        HidedRows++;
                        exAgent.HideRows(Rowi, col1, Rowi, col2);
                    }
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        public override void WriteExcelReport(string fileType)
        {
            string filename = "";
            string sheetName = "";
            string Standard = "DNVR"; 
            DataRow dwINV = null;
            if (dsSource.Tables.Contains("DNVR"))
            {
                if (dsSource.Tables["DNVR"].Rows.Count > 0)
                    dwINV = dsSource.Tables["DNVR"].Rows[0];
            }
            if (dwINV == null)
                return;
            
            strExcelFile = @"DST\DSTInventory.xls";
            string filePath = Path.Combine(PathHelper.ApplicationPath, @"Report\DST\DSTInventory.xls");
            exAgent.Open(filePath);
            exAgent.Company = strCompany;
            exAgent.strExcelFile = strExcelFile;
            string filedate = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString();
            filename = @"Sales\销售开票情况" + filedate + ".xls";
            sheetName = DateTime.Now.ToShortTimeString().Replace(":", "_").Replace(" ", "_").Replace("/", "_");
            exAgent.CreateNewSheet(filename, Standard, sheetName);
            FillInExcelData(dsSource); 
            //exAgent.Save();
            //exAgent.Close();
            //string files = Path.Combine(PathHelper.ApplicationPath, @"Report\"+filename);
            //System.Diagnostics.Process.Start("EXCEL.EXE", files);                
            base.WriteExcelReport(fileType);
        }

    }
}
